package com.cloud.shopping.cart.controller;

import com.cloud.shopping.cart.model.Cart;
import com.cloud.shopping.cart.service.CartService;
import com.cloud.shopping.tool.RespResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author John.zhang
 * @version Id: CartController, v 1.0 2021/1/3 19:37 ZhiYong Exp $
 */

@RestController
@RequestMapping(value = "/cart")
@CrossOrigin
public class CartController {

    @Resource
    private CartService cartService;

    /**
     * 指定ID集合的购物车数据
     *
     * @param ids ids
     * @return List
     */
    @PostMapping(value = "/list")
    public RespResult<List<Cart>> list(@RequestBody List<String> ids) {
        List<Cart> carts = cartService.list(ids);
        return RespResult.ok(carts);
    }

    /**
     * 增加购物车方法
     *
     * @param id  id
     * @param num num
     * @return RespResult
     */
    @GetMapping(value = "/{id}/{num}")
    public RespResult add(@PathVariable(value = "id") String id,
                          @PathVariable(value = "num") Integer num) {
        String userName = "John";
        cartService.add(id, userName, num);
        return RespResult.ok();
    }


    /**
     * 购物车列表
     *
     * @return List
     */
    @GetMapping(value = "/list")
    public RespResult<List<Cart>> list() {
        String userName = "John";
        List<Cart> list = cartService.list(userName);
        return RespResult.ok(list);
    }

    /**
     * 删除购物车数据
     *
     * @param ids ids
     * @return RespResult
     */
    @DeleteMapping
    public RespResult delete(@RequestBody List<String> ids) {
        cartService.delete(ids);
        return RespResult.ok();
    }

}
